package com.zjsru.plan2024.codeBase.fifty;

import java.util.Arrays;

/**
 * 242. 有效的字母异位词
 * @Author: cookLee
 * @Date: 2024-06-23
 */
public class IsAnagram {
    /**
     * 主
     * \
     * 输入: s = "anagram", t = "nagaram"
     * 输出: true
     * \
     * 输入: s = "rat", t = "car"
     * 输出: false
     * \
     * @param args args
     */
    public static void main(String[] args) {
        IsAnagram isAnagram = new IsAnagram();
        String s ="anagram";
        String t = "nagaram";
        System.out.println(isAnagram.isAnagram(s, t));
    }

    /**
     * 是字谜
     *
     * @param s s
     * @param t t
     * @return boolean
     */
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] sNum = new int[26];
        for (char c : s.toCharArray()) {
            sNum[c - 'a']++;
        }

        int[] tNum = new int[26];
        for (char c : t.toCharArray()) {
            tNum[c - 'a']++;
        }
        for (int i = 0; i < sNum.length; i++) {
            if (sNum[i] != tNum[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 排序
     *
     * @param s s
     * @param t t
     * @return boolean
     */
    public boolean isAnagram2(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }

        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();

        Arrays.sort(sChars);
        Arrays.sort(tChars);

        for (int i = 0; i < s.length(); i++) {
            if (sChars[i] != tChars[i]) {
                return false;
            }
        }

        return true;
    }

}
